/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *BasePermissionManageController
 * 基础权限管理
 *
 * 修改纪录
 * 2020-12-01 版本：1.0 zhanglongfei 创建。
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-12-01
 */

package com.topscomm.base.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.topscomm.base.pojo.BasePermissionManageEntity;
import com.topscomm.base.service.IBasePermissionManageService;
import com.topscomm.base.service.impl.BaseCboUserExtendService;
import com.topscomm.cbo.service.ICboDeptService;
import com.topscomm.cbo.service.ICboUserRoleService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.controller.auto.BasePermissionManageControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhanglongfei
 * @date: 2020-12-01
 * @description:基础权限管理Controller
 * @modify:
 */
@RestController
@RequestMapping("base/permissionManage")
public class BasePermissionManageController extends BasePermissionManageControllerAuto implements IBasePermissionManageController {
	@Autowired
	BaseCboUserExtendService baseCboUserExtendService;
	@Autowired
	private ICboUserService cboUserService;
	/**
	 * @author: zhanglongfei
	 * @date: 2020-12-01
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sharetype")) {
			String value = paraMap.get("qry_sharetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sharetype like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_shareobjectsdesc")) {
			String value = paraMap.get("qry_shareobjectsdesc").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and shareobjectsdesc = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourcetype")) {
			String value = paraMap.get("qry_sourcetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceid")) {
			String value = paraMap.get("qry_sourceid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceflag")) {
			String value = paraMap.get("qry_sourceflag").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceflag = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_self")) {
			sql.appendSingle(" and sourceusercodes = '{0}' ", this.getUserBean().getUser().getCode());
		}
		return sql.toString();
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2020-12-01
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:查询已有人员
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年11月3日上午9:48:36
	 * @modify:
	 */
	@GetMapping(value = "/queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			List<BasePermissionManageEntity> list = this.service.queryByWhere(sqlWhere);
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * 通过共享类型查询用户
	 *
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2020年11月4日上午10:36:35
	 * @modify:
	 */
	@GetMapping(value = "/queryUserListByShareType")
	public ResponseResult<Object> queryUserListByShareType(
			@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Query query = new Query(paraMap);
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			List<String> userCodeList = new ArrayList<>();
			String qry_deptcodes = ConvertUtil.convertToString(paraMap.get("deptcodes"));
			String qry_roleids = ConvertUtil.convertToString(paraMap.get("roleids"));
			String qry_teamids = ConvertUtil.convertToString(paraMap.get("teamids"));
			String qry_usercodes = ConvertUtil.convertToString(paraMap.get("usercodes"));
			String qry_sharecodes = ConvertUtil.convertToString(paraMap.get("sharecodes"));
			String qry_codelist = ConvertUtil.convertToString(paraMap.get("qry_codelist"));
			// 通过qry_codelist查询已选择的汇报人，只在回显已选择汇报人时使用
			if (qry_codelist != null) {
				String[] codeArray = qry_codelist.split(",");
				for (String s : codeArray) {
					userCodeList.add(s);
				}
			}
			// 通过部门查询用户
			if (qry_deptcodes.length() > 0) {
				List<Map<String, Object>> userList = baseCboUserExtendService.searchUsersByDeptCodes(qry_deptcodes);
				for (Map<String, Object> map : userList) {
					userCodeList.add(ConvertUtil.convertToString(map.get("code")));
				}
			}
			// 通过角色查询用户
			if (qry_roleids.length() > 0) {
				List<Map<String, Object>> userList = baseCboUserExtendService.searchUserByRoleIds(qry_roleids);
				for (Map<String, Object> map : userList) {
					userCodeList.add(ConvertUtil.convertToString(map.get("code")));
				}
			}
			// 选择用户本身
			if (qry_usercodes.length() > 0) {
				String[] users = qry_usercodes.split(",");
				for (String code : users) {
					userCodeList.add(code);
				}
			}
			// 按共享人
			if (qry_sharecodes.length() > 0) {
				String[] users = qry_sharecodes.split(",");
				for (String code : users) {
					userCodeList.add(code);
				}
			}
			String selectUserCodes = String.join(",", userCodeList);
			// 通過用戶code查詢用戶
			StringBufferProxy sqlTemp = new StringBufferProxy();
			if (selectUserCodes != null) {
				String[] userCodeArray = selectUserCodes.split(",");
				if (userCodeArray != null && userCodeArray.length > 0) {
					List<String> userlist = new ArrayList<String>();
					String userCodeString = "";
					for (int i = 0; i < userCodeArray.length; i = i + 1) {
						if (i != 0 && i % 1000 == 0) {
							userCodeString = userCodeString.substring(0, userCodeString.length() - 1);
							userlist.add(userCodeString);
							userCodeString = "'" + userCodeArray[i] + "'" + ",";
						} else {
							userCodeString += "'" + userCodeArray[i] + "'" + ",";
						}
					}
					if (!StringUtil.isEmpty(userCodeString)) {
						userCodeString = userCodeString.substring(0, userCodeString.length() - 1);
						userlist.add(userCodeString);
					}
					for (int i = 0; i < userlist.size(); i++) {
						if (i == 0) {
							sqlTemp.appendSingle(" code in ({0})", userlist.get(i));
						} else {
							sqlTemp.appendSingle(" or code in ({0})", userlist.get(i));
						}
					}
				}
			}
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle("1=1");
			if (!StringUtil.isEmpty(sqlTemp.toString())) {
				sql.appendSingle(" and ({0} )", sqlTemp.toString());
			}
			if (paraMap.containsKey("qry_code")) {
				String code = ConvertUtil.convertToString(paraMap.get("qry_code"));
				if (code != null) {
					sql.appendSingle(" and code like '%{0}%' ", code);
				}
			}
			if (paraMap.containsKey("qry_name")) {
				String name = ConvertUtil.convertToString(paraMap.get("qry_name"));
				if (name != null) {
					sql.appendSingle(" and name like '{0}%' ", name);
				}
			}
			int totalCount = cboUserService.getCount(sql.toString());
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = cboUserService.queryMapForPage(sql.toString(), query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else
				tempList = cboUserService.queryMapByWhere(sql.toString());
			cboUserService.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

